/*******************************************************************************
 *    系统名称   : 约汗乐动力接口/后台管理系统
 *    客户             : 约汗乐动力
 *    文件名        ： RasAesUtil.java
 *              (C) Copyright 博智乐创 Corporation 2015
 *               All Rights Reserved.
 *              http://www.bozlc.com
 * *****************************************************************************
 *    注意： 本内容仅限于约汗乐动力软件公司内部使用，未经许可禁止转发
 ******************************************************************************/
package com.xiake.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

// TODO: Auto-generated Javadoc
/**
 * The Class RasAesUtil.
 *
 * @author tony
 * @description 加解密工具类
 * @created 2014-12-17
 */
@SuppressWarnings("restriction")
public class RasAesUtil {

    /** The cipher. */
    private static Cipher cipher = null;

    /** The iv. */
    private static byte[] iv = { 0001, 0001, 0001, 0001, 0001, 0001, 0001, 0001, 0001, 0001, 0001, 0001, 0001, 0001,
            0001, 0001 };

    /** The Constant key. */
    public static final String key = "duoduokeypwd1234";

    static {
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* 加密算法 */
    /**
     * Encrypt.
     *
     * @param data
     *            the data
     * @return the string
     * @throws Exception
     *             the exception
     */
    public static String encrypt(String data) throws Exception {
        int blockSize = cipher.getBlockSize();
        byte[] dataBytes = data.getBytes();
        int plaintextLength = dataBytes.length;
        if (plaintextLength % blockSize != 0) {
            plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
        }
        byte[] plaintext = new byte[plaintextLength];
        System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
        byte[] encrypted = cipher.doFinal(plaintext);
        return encryptBASE64(encrypted).trim();
    }

    /* 解密算法 */
    /**
     * Des encrypt.
     *
     * @param data
     *            the data
     * @return the string
     * @throws Exception
     *             the exception
     */
    public static String desEncrypt(String data) throws Exception {
        byte[] encrypted1 = decryptBASE64(data);
        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
        byte[] original = cipher.doFinal(encrypted1);
        String originalString = new String(original);
        /* return originalString; */
        return originalString.trim();
    }

    /**
     * Decrypt bas e64.
     *
     * @param key
     *            the key
     * @return the byte[]
     * @throws Exception
     *             the exception
     */
    private static byte[] decryptBASE64(String key) throws Exception {
        return (new BASE64Decoder()).decodeBuffer(key);
    }

    /**
     * Encrypt bas e64.
     *
     * @param key
     *            the key
     * @return the string
     * @throws Exception
     *             the exception
     */
    private static String encryptBASE64(byte[] key) throws Exception {
        return (new BASE64Encoder()).encodeBuffer(key);
    }

    public static void main(String[] args) throws Exception {
        // String content = "qilongjava";
        // System.out.println("客户端原文="+content);
        // String jiamihou=encrypt(content);
        // System.out.println("客户端加密后="+jiamihou);
        // String encode_utf8=URLEncoder.encode(jiamihou,"UTF-8");
        // System.out.println("客户端加密后再次转码后           ="+encode_utf8);
        // String encode_utf8_two=URLEncoder.encode(encode_utf8,"UTF-8");
        // System.out.println("客户端加密后再次转码后two="+encode_utf8_two);
        // String rr = desEncrypt(jiamihou);
        // System.out.println("解密:"+rr);
        System.out
                .print(desEncrypt("UfWC2m0/kRMXXfDWbm9rZOOZIIzVawll/8wjAmH0fiBpezT9FuqJn5DkSHP8oLL4H5cWU8yRt/pb\r\nFRjnhqz6Hg=="));

    }

}